package MLMaliciousURLFinder;
import java.net.*;
import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Properties;
import java.util.*;
import Utility.Utility;

public class featureExtractor {
	
	private URL url;							//url :-)
	private String hostName;					//hostname section of url
	private String path;						//path part of url - NOTE this is currently the path defined by java
	//example: path of (https://mail.google.com/mail/?shva=1#search/oakland/1268c3b82d5d0ca8) is currently "/mail/"
	private int dotCount;						//number of '.' in the url
	private int urlLength;						//length of entire url
	private int hostNameLength;					//length of host name
	private String TLD;							//top level domain
	private String hostNameTokens[];			//tokens in host delimited by .
	private String pathTokens[];				//tokens in path delimited by "./?-_"
	private String IP;							//IP address
	private String whoisInfo;
	private String country;						//country of IP
	private String region;						//region of IP
	private String city;						//city of IP
	private boolean googleResult; 				//true - malicious, false - not malicious
	private boolean serverOrClientDomainName; 	//is "server" or "client" in the domain name?
	private boolean urlContainsIP;				//does the URL contain the IP address?
	private String whoisRegistrationDate;	//date whois was registered
	private String whoisUpdateDate;
	private String whoisExpirationDate;
	private String registrarName;
	private String registrantName;
	private String domainName;
	String primaryDomainName;
	boolean registrationLocked;
	private static int MAX_BATCH_SIZE = 50;
	
	//Map map = new HashMap();
	//map<String> featureValuePairs = new HashMap<String>();
	Map<Integer, String> featureValuePairs;

	public String getWhoisRegistrationDate()
	{
		return whoisRegistrationDate;
	}

	public String getWhoisUpdateDate()
	{
		return whoisUpdateDate;
	}
	
	public String getWhoisExpirationDate()
	{
		return whoisExpirationDate;
	}
	
	public String getRegistrarName()
	{
		return registrarName;
	}
	
	public String getRegistrantName()
	{
		return registrantName;
	}

	public boolean getUrlContainsIP() {
		return urlContainsIP;
	}

	public boolean getServerOrClientDomainName() {
		return serverOrClientDomainName;
	}

	public boolean getGoogleResult() {
		return googleResult;
	}

	public String getCountry() {
		return country;
	}

	public String getCity() {
		return city;
	}	
	
	public String getWhoisInfo() {
		return whoisInfo;
	}

	public String getTLD() {
		return TLD;
	}

	public String getHostName() {
		return hostName;
	}

	public String getPath() {
		return path;
	}

	public int getDotCount() {
		return dotCount;
	}

	public int getUrlLength() {
		return urlLength;
	}

	public int getHostNameLength() {
		return hostNameLength;
	}
	
	public String getRegion() {
		return region;
	}
	
	public void printHostNameTokens()
	{
		for(int i=0; i<hostNameTokens.length; i++)
		{
			System.out.print(hostNameTokens[i]+" ");
		}
		System.out.println();
	}
	
	public void printPathTokens()
	{
		for(int i=0; i<pathTokens.length; i++)
		{
			System.out.print(pathTokens[i]+" ");
		}
		System.out.println();
	}
	
	featureExtractor(String url_string) throws MalformedURLException, UnknownHostException
	{
		url = new URL(url_string);
		hostName = url.getHost();
		path = url.getPath();
		dotCount = calcDotCount();
		urlLength = url_string.length();
		hostNameLength = hostName.length();
		//TLD = parseTLD();
		parsePrimaryDomainName();
		obtainDomainName(hostName);
		//hostNameTokens = tokenizeHostName();
		//pathTokens = tokenizePath();
		IP = retreiveIP();
		//whoisInfo = retreiveWhoisInfo();
		//retreiveGeoInfo();
		//googleResult = retreiveGoogleResult();
		serverOrClientDomainName = retreiveServerOrClientDomainName();
		urlContainsIP = retreiveUrlContainsIP();
		retreiveWhoisInfo();
	}
	
	private void parsePrimaryDomainName()
	{
		primaryDomainName = hostName;
		int last_dot = 0;
		int first_dot = -1;
		while (last_dot != first_dot)
		{
			last_dot = primaryDomainName.lastIndexOf('.');
			first_dot = primaryDomainName.indexOf('.');
			if (first_dot != last_dot)
			{
				primaryDomainName = primaryDomainName.substring(first_dot+1);
			}
		}
		if(primaryDomainName.indexOf(".") != -1)
		{
			primaryDomainName = primaryDomainName.substring(0, primaryDomainName.indexOf("."));
		}
	}

	private void obtainDomainName(String hostname)
	{
		domainName = hostname;
		int last_dot = 0;
		int first_dot = -1;
		while (last_dot != first_dot)
		{
			last_dot = domainName.lastIndexOf('.');
			first_dot = domainName.indexOf('.');
			if (first_dot != last_dot)
			{
				domainName = domainName.substring(first_dot+1);
			}
		}

	}
	
	private void retreiveWhoisInfo()
	{
		// Default server is whois.geektools.com
		String server = "whois.verisign-grs.com";
		// Default server port is 43
		int port = 43;

		// Load the properties file.
		try
		{
			FileInputStream in = new FileInputStream("Whois.properties");
			Properties app = new Properties();

			app.load(in);

			// Get the server property
			server = (app.getProperty("server", server));

			// Get the port property
			try
			{
				port = Integer.parseInt(app.getProperty("port"));
			}
			catch (NumberFormatException e)
			{
				// Do nothing!
			}

			in.close();
		}
		catch (FileNotFoundException e)
		{
			// Do nothing!
		}
		catch (IOException e)
		{

			Utility.logMessage("Whois: an error occurred while loading the properties file: " + e, "featureExtractor_errorlog.txt");
		}

		// Build the whois query using command line arguments
		//System.out.println("LOOKING UP " + domainName);
		StringBuffer buff = new StringBuffer("="+domainName);

		// Convert string buffer to string
		String query = buff.toString();
		
		// The whois server can be specified after "@"
		// e.g.: query@whois.networksolutions.com
		int at = query.lastIndexOf("@");
		int len = query.length();

		if ((at != -1))
		{
			// Remove the @ if last character in query
			// e.g.: john@doe.com@
			if (at == (len - 1))
			{
				query = query.substring(0, len - 1);
			}
			else
			{
				// The whois server is specified after "@"
				server = query.substring(at + 1, len);
				// The whois query is specified before "@"
				query = query.substring(0, at);
			}
		}

		try
		{
			// Establish connection to whois server & port
			Socket connection = new Socket(server, port);
			PrintStream out = new PrintStream(connection.getOutputStream());
			BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			String line = "";

			// Send the whois query
			out.println(query);
			// Display the whois server's address & port
			//System.out.println("[" + server + ":" + port + "]");

			int index = 0;
			String dateString = "";
			// Read/display the query's result
			while ((line = in.readLine()) != null)
			{
				//System.out.println(line);
				
				if(line.indexOf("Creation Date:") != -1)
				{
					index = line.indexOf(':');					
					dateString = line.substring(index+1);
					//System.out.println(dateString);
					whoisRegistrationDate = convertDate(dateString);
				}
				else if((index=line.indexOf("Expiration Date:")) >=0)
				{
					index = line.indexOf(':');
					dateString = line.substring(index+1);
					whoisExpirationDate = convertDate(dateString);
				}
				else if((index=line.indexOf("Updated Date:")) >=0)
				{
					index = line.indexOf(':');
					dateString = line.substring(index+1);
					whoisUpdateDate = convertDate(dateString);
				}
				else if((index=line.indexOf("Registrar:")) >=0)
				{
					registrarName = line.substring(line.indexOf(":")+1);
					registrarName = registrarName.replaceAll(" ", "");
					registrarName = registrarName.replaceAll("	", "");
					registrarName = registrarName.toLowerCase();
				}
			}
		}
		catch (java.net.UnknownHostException e)
		{
			// Unknown whois server
			System.err.println("Whois: unknown host: " + server);

			return;
		}
		catch (IOException e)
		{
			// Could not connect to whois server
			System.err.println("Whois: " + e);

			return;
		}
	}

	private String convertDate(String date)
	{
		
		String year;
		String month;
		String day;
		int year_i;
		int month_i;
		int day_i;
		// need to extract month, day and year

		date = date.replaceAll(" ", "");
		//System.out.println("DATE: " + date);
		int hyphen_index = date.indexOf('-');
		
		if (hyphen_index > 2)
		{
			year = date.substring(0,4);
			month = date.substring(5,7);
			//System.out.println(year + " " + month);
			//day = date.substring(8, date.indexOf('.'));
			day = date.substring(8, 10);
			month_i = Integer.parseInt(month.replaceAll(" ", ""));
		}
		else
		{
			day = date.substring(0,2);
			month = date.substring(3,6);
			year = date.substring(7);
			month_i = convertMonth(month);
		}
		year_i = Integer.parseInt(year.replaceAll(" ", ""));
		day_i = Integer.parseInt(day.replaceAll(" ", ""));

		if(year_i < 50)
		{
			year_i += 2000;
		}
		else if(year_i < 100)
		{
			year_i += 1900;
		}

		//Timestamp time = Timestamp(year_i, month_i, day_i, 0, 0, 0, 0);
		
		long time = year_i - 1970;
		time = time * 365 * 24 * 60 * 60;
		time += month_i * 30 * 24 * 60 * 60;
		time += day_i * 24 * 60 * 60;
		
		return String.valueOf(time);
	}

	private int convertMonth(String month)
	{
		month = month.toLowerCase();
		if(month.startsWith("jan"))
			return 1;
		else if(month.startsWith("feb"))
			return 2;
		else if(month.startsWith("mar"))
			return 3;
		else if(month.startsWith("apr"))
			return 4;
		else if(month.startsWith("may"))
			return 5;
		else if(month.startsWith("jun"))
			return 6;
		else if(month.startsWith("jul"))
			return 7;
		else if(month.startsWith("aug"))
			return 8;
		else if(month.startsWith("sep"))
			return 9;
		else if(month.startsWith("oct"))
			return 10;
		else if(month.startsWith("nov"))
			return 11;
		else if(month.startsWith("dec"))
			return 12;
		else
		{
			System.out.println("ERROR!!!");
			System.exit(1);
			return 13;
		}
	}
	
	//returns true if url contains IP, false otherwise
	private boolean retreiveUrlContainsIP()
	{
		if(url.toString().indexOf(IP) != -1)
		{
			return true;
		}
		else
		{
			String[] atoms = IP.split("\\.");
	        String hexIP = "";
	        for(int i=0; i<atoms.length; i++)
	        {
	        	hexIP += Long.toHexString(Long.parseLong(atoms[i]));
	        	hexIP += ".";
	        }
	        hexIP = hexIP.substring(0, hexIP.length()-1);
	        if(url.toString().indexOf(hexIP) != -1)
	        {
	        	return true;
	        }
	    }
		return false;
	}
	
	//returns true if url contains "client" or "server"
	private boolean retreiveServerOrClientDomainName()
	{
		if(url.toString().indexOf("client") != -1 ||
		   url.toString().indexOf("server") != -1)
		{
			return true;
		}
		return false;
	}
	
	/*  
	 *  visits google's diagnostic site with url as parameter,
	 *  looks for string that indicates the page is not malicious,
	 *  if found, returns false, otherwise true
	 */
	private boolean retreiveGoogleResult()
	{
		String s; //line of response
		String googleUrlString = "http://www.google.com/safebrowsing/diagnostic?site=" + url.toString();
		try
		{
			URL googleUrl = new URL(googleUrlString);
			HttpURLConnection connection = (HttpURLConnection)googleUrl.openConnection();
			connection.connect();
			InputStream inputStream = (InputStream)connection.getContent();
			BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
			while((s = reader.readLine())!= null)
			{
				if(s.indexOf("This site is not currently listed as suspicious.") >=0)
				{
					return false;
				}
			}			
		}
		catch(IOException ioe)
		{
			System.out.println("Error in getting google diagnostics info");
		}
		return true;		
	}
	
	private String retreiveIP()
	{
		try
		{
			String IPString = InetAddress.getByName(hostName).toString();
			return IPString.substring(IPString.indexOf('/')+1);
		}
		catch(UnknownHostException uhe)
		{
			return "255.255.255.255";
		}
	}
	
	/*
	 * Visits geoiptool website with ipaddress as parameter,
	 * scrapes country, region and city info from content
	 */
	private void retreiveGeoInfo()
	{
		String s; //line of response
		String geoUrlString = "http://www.geoiptool.com/en/?IP=" + IP;
		int begin_ndx, end_ndx;
		try
		{
			URL GeoUrl = new URL(geoUrlString);
			HttpURLConnection connection = (HttpURLConnection)GeoUrl.openConnection();
			connection.connect();
			InputStream inputStream = (InputStream)connection.getContent();
			BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
			while((s = reader.readLine())!= null)
			{
				if(s.indexOf("Country:") >=0)
				{
					country = reader.readLine();
					begin_ndx = country.indexOf("target=\"_blank\">")+17;
					end_ndx = country.indexOf("</a>");
					country = country.substring(begin_ndx, end_ndx);
				}
				if(s.indexOf("City:") >=0)
				{
					city = reader.readLine();
					begin_ndx = city.indexOf("bold\">")+6;
					end_ndx = city.indexOf("</td>");
					city = city.substring(begin_ndx, end_ndx);
				}
				if(s.indexOf("Region:") >=0)
				{
					region = reader.readLine();
					begin_ndx = region.indexOf("target=\"_blank\">")+16;
					end_ndx = region.indexOf("</a>");
					region = region.substring(begin_ndx, end_ndx);
				}
			}
		}
		catch(IOException ioe)
		{
			System.out.println("Error in getting geo info");
		}
	}
	
	private String[] tokenizeHostName()
	{
		String splitHostName = hostName.replace('.', ' ');
		return splitHostName.split(" ");
	}
	
	private String[] tokenizePath()
	{
		String splitPath = path.replace('.', ' ');
		splitPath = splitPath.replace('/', ' ');
		splitPath = splitPath.replace('?', ' ');
		splitPath = splitPath.replace('=', ' ');
		splitPath = splitPath.replace('-', ' ');
		splitPath = splitPath.replace('_', ' ');
		return splitPath.split(" ");
	}
	
	private String parseTLD()
	{
		int index_last_dot = hostName.lastIndexOf('.');
		return hostName.substring(index_last_dot+1);
	}
	
	private int calcDotCount()
	{
		int count = 0;
		for(int i=hostName.indexOf('.'); i != -1; i = hostName.indexOf('.', i+1))
		{
			count++;
		}
		return count;
	}
	
	public void createDictionary()
	{
		int num_features = 1;
		featureValuePairs = new HashMap<Integer, String>();
		
		if (dotCount != 0) {
			featureValuePairs.put(num_features, String.valueOf(dotCount));
		}
		num_features++;
		
		
		if(urlContainsIP){
			featureValuePairs.put(num_features, "1");
		}
		num_features++;
		
		if(whoisRegistrationDate != null && !whoisRegistrationDate.equals("0")) {
			featureValuePairs.put(num_features, whoisRegistrationDate);
		}
		num_features++;
		
		if(whoisExpirationDate != null && !whoisExpirationDate.equals("0")) {
			featureValuePairs.put(num_features, whoisExpirationDate);
		}
		num_features++;
		
		if(whoisRegistrationDate != null && !whoisRegistrationDate.equals("0")) {
			featureValuePairs.put(num_features, whoisRegistrationDate);
		}
		num_features++;
		
		if(serverOrClientDomainName){
			featureValuePairs.put(num_features, "1");
		}
		num_features++;
		
		if (hostNameLength != 0) {
			featureValuePairs.put(num_features, String.valueOf(hostNameLength));
		}
		num_features++;
		
		if (urlLength != 0) {
			featureValuePairs.put(num_features, String.valueOf(urlLength));
		}
		num_features++;
	}

	public void appendToFile(String filename, String malicious)
	{
		this.createDictionary();
		try
		{
			BufferedWriter out = new BufferedWriter(new FileWriter(filename, true));
			out.write(malicious + " ");
			Set<Integer> keys = featureValuePairs.keySet();
			for(java.util.Iterator<Integer> keyIterator = keys.iterator(); keyIterator.hasNext();)
			{
				int key = keyIterator.next();
				out.write(key + ":" + featureValuePairs.get(key) + " ");
			}
			out.write("\n");
			out.close();
		}
		catch(IOException e)
		{
			System.out.println("ERROR: file not found: " + filename);
			System.exit(1);
		}
	}
	
	public static void extractFromFile(String input_filename, String output_filename)
	{
		String line;
		String[] line_contents;
		String malicious;
		String url;
		try {
		      BufferedReader input =  new BufferedReader(new FileReader(input_filename));
		      try {
		        while (( line = input.readLine()) != null){
		        	line_contents = line.split(" ");
		        	if(line_contents[0].equals("1"))
		        	{
		        		malicious = "0";
		        	}
		        	else
		        	{
		        		malicious = "1";
		        	}
					url = line_contents[1];
					featureExtractor fe = new featureExtractor(url);
					fe.appendToFile(output_filename, malicious);
		        }
		      }
		      finally {
		    	  input.close();
		      }
		}
		catch (IOException ex){
		  ex.printStackTrace();
		}
	}
	
	public static void processURLs(int start)
	{
		System.out.println("Process URLS");
		PreparedStatement psInstURLMap = null;
		try {
			Class.forName("org.sqlite.JDBC");
			Connection conn = DriverManager.getConnection("jdbc:sqlite:twitter.db");
			Statement stmt = conn.createStatement();
			ResultSet srs = stmt.executeQuery("SELECT id, tweet_table_id, url FROM urls WHERE id >= " + start + " LIMIT " + MAX_BATCH_SIZE);

			int id = 0;
			String url = "";
			int tweet_table_id = 0;
			while(srs.next()) {
				id = srs.getInt("id");
				url = srs.getString("url");
				tweet_table_id = srs.getInt("tweet_table_id");
				storeURL(url, tweet_table_id);
				String instURLMap = "UPDATE urls SET status = 'E' WHERE id = ?;";
				psInstURLMap = conn.prepareStatement(instURLMap);
				psInstURLMap.setInt(1, id);
				psInstURLMap.addBatch();
			}
			psInstURLMap.executeBatch();
			psInstURLMap.clearBatch();
			stmt.close();
			srs.close();
			conn.close();
		}
		catch(SQLException e){
			e.printStackTrace();
			Utility.logMessage("ERROR: extracting from " + start + " to " + (start+MAX_BATCH_SIZE) + e.getMessage(), "featureExtractor_errorlog.txt");
		}
		catch(Exception e){
			e.printStackTrace();
			Utility.logMessage("ERROR: extracting from " + start + " to " + (start+MAX_BATCH_SIZE) + e.getMessage(), "featureExtractor_errorlog.txt");
		}
	}
	
	public static Vector<String> getUserInfo(int tweet_table_id)
	{
		System.out.println("get user info");
		int user_id = 0;
		String screen_name = "";
		try {
			Class.forName("org.sqlite.JDBC");
			Connection conn = DriverManager.getConnection("jdbc:sqlite:twitter.db");
			Statement stmt = conn.createStatement();
			ResultSet srs = stmt.executeQuery("SELECT user_id FROM tweets WHERE id = " + tweet_table_id);

			srs.next();
			if(!srs.isAfterLast())
			{
				user_id = srs.getInt("user_id");
			}
			else
			{
				Utility.logMessage("Error: could not find user id for tweet_table_id: " + tweet_table_id, "featureExtractor_errorlog.txt");
			}
			stmt.close();
			srs.close();
			
			stmt = conn.createStatement();
			srs = stmt.executeQuery("SELECT screen_name FROM users WHERE user_id = " + user_id);
			srs.next();
			if(!srs.isAfterLast())
			{
				screen_name = srs.getString("screen_name");
			}
			else
			{
				Utility.logMessage("Error: could not find screen_name for tweet_table_id: " + tweet_table_id, "featureExtractor_errorlog.txt");
			}
			stmt.close();
			srs.close();
			conn.close();
		}
		catch(SQLException e){
			e.printStackTrace();
			Utility.logMessage("ERROR: cant get user info for tweet_table_id: " + tweet_table_id + e.getMessage(), "featureExtractor_errorlog.txt");
		}
		catch(Exception e){
			e.printStackTrace();
			Utility.logMessage("ERROR: cant get user info for tweet_table_id: " + tweet_table_id + e.getMessage(), "featureExtractor_errorlog.txt");
		}
		
		Vector<String> userInfo = new Vector<String>();
		userInfo.add(screen_name);
		userInfo.add(String.valueOf(user_id));
		return userInfo;
	}
	
	public static void storeURL(String url, int tweet_table_id)
	{
		//System.out.println("Storing url: " + url);
		Vector<String> userInfo = getUserInfo(tweet_table_id);
		
		Utility.logMessage(userInfo.get(0) + " " + userInfo.get(1) + " " + tweet_table_id, "url_info.txt");
		try {
			featureExtractor fe = new featureExtractor(url);
			fe.appendToFile("url_features.txt", "0");
		}
		catch (UnknownHostException e)
		{
			Utility.logMessage("Error: could not extract features for url: " + url + e.getMessage(), "featureExtractor_errorlog.txt");
		}
		catch (MalformedURLException e)
		{
			Utility.logMessage("Error: could not extract features for url: " + url + e.getMessage(), "featureExtractor_errorlog.txt");
		}		
	}
	
	public static void extractFromDB()
	{
		System.out.println("extractFromDB");
		//get starting id from file
		int startingID = 0;		
		
		//get ending id from url table
		int endingID = 0;
		try {
			Class.forName("org.sqlite.JDBC");
			Connection conn = DriverManager.getConnection("jdbc:sqlite:twitter.db");
			
			Statement stmt = conn.createStatement();
			ResultSet srs = stmt.executeQuery("SELECT id FROM urls WHERE status = 'E' ORDER BY id DESC LIMIT 1");
			srs.next();
			if (!srs.isAfterLast())
			{
				startingID = srs.getInt("id");
			}
			stmt.close();
			srs.close();
			
			stmt = conn.createStatement();
			srs = stmt.executeQuery("SELECT id FROM urls ORDER BY id DESC LIMIT 1");
			srs.next();
			endingID = srs.getInt("id");
			stmt.close();
			srs.close();
			
			conn.close();
		}
		catch(SQLException e){
			e.printStackTrace();
			Utility.logMessage("Error extracting final id from urls table", "featureExtractor_errorlog.txt");
		}
		catch(Exception e){
			e.printStackTrace();
			Utility.logMessage("Error extracting final id from urls table", "featureExtractor_errorlog.txt");
		}
		
		int currentID = startingID;
		System.out.println("Starting: " + startingID + " Ending: " + endingID);
		while(currentID < endingID)
		{
			processURLs(currentID);
			currentID += MAX_BATCH_SIZE;
		}
	}
	
	public static void main(String args[])
	{
		//System.out.println("Hello, World!");
		//featureExtractor.extractFromDB();
		featureExtractor.extractFromFile("url_data.txt", "training_urls.txt");
		
//		try
//		{
//			//featureExtractor fe = new featureExtractor("http://www.cad-online.com");
//			featureExtractor fe = new featureExtractor("http://www.wikipedia.org");
//			//featureExtractor fe = new featureExtractor("http://www.google.com");
//			
//			System.out.println("Registrant " + fe.getRegistrantName());
//			System.out.println("Registrar " + fe.getRegistrarName());
//			System.out.println("Reg Date " + fe.getWhoisRegistrationDate());
//			System.out.println("Expire Date " + fe.getWhoisExpirationDate());
//			System.out.println("Update Date " + fe.getWhoisUpdateDate());
//		}
//		catch(MalformedURLException mue)
//		{
//			Utility.logMessage("Error in main" + mue, "featureExtractor_errorlog.txt");
//		}
//		catch(UnknownHostException uhe)
//		{
//			Utility.logMessage("Error in main" + uhe, "featureExtractor_errorlog.txt");
//		}
		
		/*
		//String input = "https://mail.google.com/mail/?shva=1#search/oakland/1268c3b82d5d0ca8";
		String input = "http://en.wikipedia.org";
		System.out.println(input);
		try
		{
			featureExtractor fe = new featureExtractor(input);
			System.out.println("Hostname: " + fe.getHostName());
			//System.out.println("Path: " + fe.getPath());
			System.out.println("Dot Count: " + fe.getDotCount());
			System.out.println("URL length: " + fe.getUrlLength());
			System.out.println("Hostname Length: " + fe.getHostNameLength());
			//System.out.println("TLD: "+ fe.getTLD());
			//fe.printHostNameTokens();
			//fe.printPathTokens();
			System.out.println("IP Address: " + fe.IP.toString());
			//System.out.println("WHOIS INFO:");
			System.out.print(fe.getWhoisInfo());
			//System.out.println("Country: " + fe.getCountry());
			//System.out.println("Region: " + fe.getRegion());
			//System.out.println("City: " + fe.getCity());
			//System.out.println("Google Diagnostic result: " + fe.getGoogleResult());
			System.out.println("URL contains \"client\" or \"server\": " + fe.getServerOrClientDomainName());
			System.out.println("URL contains IP: " + fe.getUrlContainsIP());
			System.out.println("Whois Registration Date: " + fe.getWhoisRegistrationDate());
			//System.out.println("Whois Update Date: " + fe.getWhoisUpdateDate());
			//System.out.println("Whois Expiration Date: " + fe.getWhoisExpirationDate());
			//System.out.println("Whois Registrar Name: " + fe.getRegistrarName());
			//System.out.println("Whois Registrant Name: " + fe.getRegistrantName());
			System.out.println("Primary Domain Name: " + fe.primaryDomainName);

			fe.appendToFile("file.txt", "?");
		}
		catch(MalformedURLException mue)
		{
			System.out.println("Error: Malformed url: " + input);
		}
		catch(UnknownHostException uhe)
		{
			System.out.println("Error: Unknown host: " + input);
		}
		*/
	}
}
